package com.cbgolf.oa.model;

import android.app.Activity;
import android.content.Intent;

import com.cbgolf.oa.base.BaseModel;
import com.cbgolf.oa.presenter.IBillPresenter;
import com.cbgolf.oa.entity.BillBean;
import com.cbgolf.oa.net.NetCallBack;
import com.cbgolf.oa.net.Web;
import com.cbgolf.oa.net.WebAPI;
import com.cbgolf.oa.util.DataUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lq on 2017/11/2.
 */

public class BillHistoryModel extends BaseModel {

    private String pointCode;
    private IBillPresenter worker;

    public BillHistoryModel(Activity act, IBillPresenter worker) {
        this.worker = worker;
        super.init(act);
    }

    @Override
    protected void getInitData() {
        Intent intent = context.getIntent();
        pointCode = DataUtil.getStringExtra(intent, "pointCode");
        getData(INITDATA);
    }

    public void getData(final int type) {
        String url = WebAPI.billing_history_post;
        Map<String, Object> map = new HashMap<>();
        JSONObject json = new JSONObject();
        try {
            json.put("currentPage", (type == LOADMORE ? String.valueOf(++page) : "0"));
            json.put("pageSize", pageSize);
            json.put("orderPlaceCode", pointCode);
//            json.put("consumerCarNo", cardNum);
//            json.put("orderTime", TextUtil.Text(orderTime));
            json.put("userAccount", DataUtil.getUserAccount());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        map.put("token", DataUtil.getToken());
        map.put("dto", json);
        worker.showProgress();
        Web.postJsonOk(url, map, new NetCallBack(response -> {
//            worker.stopProgress();
//            if (200 != response.statusCode) {
//                setRequestFail(type, response.errorMsg);
//                return;
//            }
//            List<BillBean> list = null;
//            try {
//                com.alibaba.fastjson.JSONObject json1 = JSON.parseObject(response.result);
//                list = JSON.parseArray(DataUtil.getJsonString(json1, "content"), BillBean.class);
//            } catch (Exception e) {
//                setRequestFail(type, ErrorUtil.NETERROR);
//                return;
//            }
//            setRequestSuccess(type, list);
            listResult(response,BillBean.class,type,worker);
        }));
    }
//
//    private void setRequestSuccess(int type, List<BillBean> list) {
//        switch (type) {
//            case INITDATA:
//                worker.getDataSuccess(list);
//                break;
//            case LOADMORE:
//                worker.loadMoreSuccess(list);
//                break;
//            case REFRESH:
//                worker.refershSuccess(list);
//                break;
//        }
//
//    }

//    private void setRequestFail(int type, String errorMsg) {
//        switch (type) {
//            case INITDATA:
//                worker.getDataFail(errorMsg);
//                break;
//            case LOADMORE:
//                reducePage();
//                worker.loadMoreFail();
//                break;
//            case REFRESH:
//                worker.refreshFail(errorMsg);
//                break;
//        }
//    }

//    private void reducePage() {
//        page = ((page > 0) ? --page : 0);
//    }
}
